package com.zhuchaooasystem.api;


import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by cc on 16/7/20.
 */
public class RxResultHelper {


    public static <T> Observable.Transformer<T, T> handleResult() {


        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> stringObservable) {
                return stringObservable.flatMap(new Func1<T, Observable<T>>() {
                    @Override
                    public Observable<T> call(T s) {
                        if (s != null) {
                            //返回数据是一个实体类
//                            Logger.e("返回数据=" + s.toString());
                            return createData(s);
                        } else {

                            return Observable.empty();

                        }
//                        else{
//
//                            return createData(s);
//                        }
//                        else {
//
//                            return Observable.error(new ServerException(s.getMsg()));
//                        }
                    }
                });
            }
        };

    }


    public static <T> Observable<T> createData(final T t) {

        return Observable.create(new Observable.OnSubscribe<T>() {
            @Override
            public void call(Subscriber<? super T> subscriber) {
                try {
                    subscriber.onNext(t);
                    subscriber.onCompleted();
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        });

    }
}

